package one.bugu.android.demon.ui.activity;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.juefeng.android.framework.LKUtil;
import com.juefeng.android.framework.view.annotation.LKContentView;
import com.juefeng.android.framework.view.annotation.LKInjectView;

import java.util.HashMap;
import java.util.Map;

import one.bugu.android.demon.R;
import one.bugu.android.demon.common.BaseHttpAsycResponceHandler;
import one.bugu.android.demon.common.MyBaseActivity;
import one.bugu.android.demon.constant.Constant;
import one.bugu.android.demon.constant.HttpConstant;
import one.bugu.android.demon.util.IntentUtils;
import one.bugu.android.demon.util.PreferencesUtil;
import one.bugu.android.demon.util.StatusBarUtils;

/**
 * 新人引导输入邀请码界面
 */
@LKContentView(R.layout.activity_invite)
public class InviteActivity extends MyBaseActivity {

    @LKInjectView(R.id.ll_invite_layout)
    private LinearLayout llInviteLayout;

    /**
     * 邀请码编辑框
     */
    @LKInjectView(R.id.invite_invitationCode)
    private EditText inviteNum;

    /**
     * 跳过按钮
     */
    @LKInjectView(R.id.invite_jump)
    private LinearLayout btnJump;

    /**
     * 确定按钮
     */
    @LKInjectView(R.id.invite_btnOk)
    private Button btnOk;

    String token;

    @Override
    protected void initView() {
        super.initView();
        StatusBarUtils.getInstance().setStatusBar("#FFFFFF", this, llInviteLayout, true);
        token = PreferencesUtil.getInstance().getString(InviteActivity.this, Constant.TOKEN);

    }

    @Override
    protected void initData() {
        super.initData();
    }

    /**
     * 验证邀请码
     */
    public void bandingInviteCode() {

        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        map.put("parentInviteCode", inviteNum.getText().toString());

        LKUtil.getHttpManager().postBody(HttpConstant.INVITECODE_URL, map, handler);
    }

    /**
     * 验证邀请码网络请求
     */
    @SuppressLint("HandlerLeak")
    public BaseHttpAsycResponceHandler<String> handler = new BaseHttpAsycResponceHandler<String>(true) {

        @Override
        public void onSuccess(String data) {
            super.onSuccess(data);
            Toast.makeText(InviteActivity.this, "绑定成功", Toast.LENGTH_SHORT).show();
            //邀请码有效 跳转下个页面
            IntentUtils.startAty(InviteActivity.this, WarnMainActivity.class);
            finish();
        }

        @Override
        public void onFailed(int code, String message) {
            super.onFailed(code, message);
            if (code == -1) {

                Toast.makeText(InviteActivity.this, "邀请码无效", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onError() {
            super.onError();
        }
    };

    @Override
    protected void initEvent() {
        super.initEvent();

        //确定点击事件
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (inviteNum.getText().toString().equals("") || inviteNum.getText().toString() == null) {

                    Toast.makeText(InviteActivity.this, "请输入有效验证码", Toast.LENGTH_SHORT).show();
                } else {

                    bandingInviteCode();
                }

            }
        });

        //跳过点击事件, 跳转下个引导页
        btnJump.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                IntentUtils.startAty(InviteActivity.this, WarnMainActivity.class);
                finish();
            }
        });
    }
}
